﻿// -----------------------------------------------------------------------
// <copyright file="SiteLogData.cs" company="VDC-IT" author="LTDUC">
//      Copyright (c) VDC-IT. All rights reserved.
// </copyright>
// -----------------------------------------------------------------------

namespace VDC.VONE.DAL
{
    using System;
    using System.Collections.Generic;
    using System.Data;
    using System.Data.SqlClient;
    using System.Reflection;
    using Microsoft.ApplicationBlocks.Data;
    using VDC.COMMON;
    using VDC.COMMON.NonPersistents;
    using VDC.VONE.ENTITIES;
    
    /// <summary>
    /// Class access to db SiteLog
    /// </summary>
    public class SiteLogData
    {
        /// <summary>
        /// Get SiteLog By Id
        /// </summary>
        /// <param name="siteLogId">Param siteLogId</param>
        /// <returns>Return SiteLog</returns>
        public SiteLogEN GetSiteLogById(int siteLogId)
        {
            string methodName = MethodBase.GetCurrentMethod().DeclaringType.ToString() + Constants.Dot + MethodBase.GetCurrentMethod().Name;
            LogHelper.Instance.WriteInfo(Constants.Begin + methodName);
            try
            {
                SiteLogEN siteLogEN = null;
                var reader = SqlHelper.ExecuteReader(SqlHelper.ConnectionString, Constants.StorepProcedure.VOneSiteLogGetById, siteLogId);
                while (reader.Read())
                {
                    siteLogEN = new SiteLogEN();

                    if (!reader.IsDBNull(0))
                    {
                        siteLogEN.SiteLogId = reader.GetDecimal(0);
                    }

                    if (!reader.IsDBNull(1))
                    {
                        siteLogEN.UserName = reader.GetString(1);
                    }

                    if (!reader.IsDBNull(2))
                    {
                        siteLogEN.Email = reader.GetString(2);
                    }

                    if (!reader.IsDBNull(3))
                    {
                        siteLogEN.Browser = reader.GetString(3);
                    }

                    if (!reader.IsDBNull(4))
                    {
                        siteLogEN.PlatForm = reader.GetString(4);
                    }

                    if (!reader.IsDBNull(5))
                    {
                        siteLogEN.IpAddress = reader.GetString(5);
                    }

                    if (!reader.IsDBNull(6))
                    {
                        siteLogEN.SubmitedTime = reader.GetDateTime(6);
                    }
                }

                reader.Close();
                reader.Dispose();
                LogHelper.Instance.WriteInfo(Constants.End + methodName);
                return siteLogEN;
            }
            catch (SqlException ex)
            {
                LogHelper.Instance.WriteError(methodName + Constants.Colon + ex.Message);
                throw new Exception(ex.ToString());
            }
        }

        /// <summary>
        /// Get list all SiteLog
        /// </summary>
        /// <returns>Return list all SiteLog</returns>
        public IList<SiteLogEN> GetSiteLogs()
        {
            string methodName = MethodBase.GetCurrentMethod().DeclaringType.ToString() + Constants.Dot + MethodBase.GetCurrentMethod().Name;
            LogHelper.Instance.WriteInfo(Constants.Begin + methodName);
            try
            {
                IList<SiteLogEN> lstSiteLog = new List<SiteLogEN>();
                SqlDataReader reader = SqlHelper.ExecuteReader(SqlHelper.ConnectionString, Constants.StorepProcedure.VOneSiteLogGetAll);
                while (reader.Read())
                {
                    var siteLog = new SiteLogEN();

                    if (!reader.IsDBNull(0))
                    {
                        siteLog.SiteLogId = reader.GetDecimal(0);
                    }

                    if (!reader.IsDBNull(1))
                    {
                        siteLog.UserName = reader.GetString(1);
                    }

                    if (!reader.IsDBNull(2))
                    {
                        siteLog.Email = reader.GetString(2);
                    }

                    if (!reader.IsDBNull(3))
                    {
                        siteLog.Browser = reader.GetString(3);
                    }

                    if (!reader.IsDBNull(4))
                    {
                        siteLog.PlatForm = reader.GetString(4);
                    }

                    if (!reader.IsDBNull(5))
                    {
                        siteLog.IpAddress = reader.GetString(5);
                    }

                    if (!reader.IsDBNull(6))
                    {
                        siteLog.SubmitedTime = reader.GetDateTime(6);
                    }

                    lstSiteLog.Add(siteLog);
                }

                reader.Close();
                reader.Dispose();
                LogHelper.Instance.WriteInfo(Constants.End + methodName);
                return lstSiteLog;
            }
            catch (SqlException ex)
            {
                LogHelper.Instance.WriteError(methodName + Constants.Colon + ex.Message);
                throw new Exception(ex.ToString());
            }
        }

        /// <summary>
        /// Insert to db SiteLog
        /// </summary>
        /// <param name="siteLog">Param SiteLog</param>
        /// <returns>Return Fail: 0, Success: @@Identity</returns>
        public int Insert(SiteLogEN siteLog)
        {
            string methodName = MethodBase.GetCurrentMethod().DeclaringType.ToString() + Constants.Dot + MethodBase.GetCurrentMethod().Name;
            LogHelper.Instance.WriteInfo(Constants.Begin + methodName);
            try
            {
                var sqlParameters = new[]
                                {
                                    new SqlParameter(Constants.SqlParamerterName.SiteLog.UserName, siteLog.UserName),
                                    new SqlParameter(Constants.SqlParamerterName.SiteLog.Email, siteLog.Email),
                                    new SqlParameter(Constants.SqlParamerterName.SiteLog.Browser, siteLog.Browser),
                                    new SqlParameter(Constants.SqlParamerterName.SiteLog.PlatForm, siteLog.PlatForm),
                                    new SqlParameter(Constants.SqlParamerterName.SiteLog.IpAddress, siteLog.IpAddress),
                                    new SqlParameter(Constants.SqlParamerterName.SiteLog.SubmitedTime, siteLog.SubmitedTime)
                                };

                var result = Convert.ToInt32(SqlHelper.ExecuteScalar(SqlHelper.ConnectionString, Constants.StorepProcedure.VOneSiteLogInsert, sqlParameters));
                LogHelper.Instance.WriteInfo(Constants.End + methodName);
                return result;
            }
            catch (SqlException ex)
            {
                LogHelper.Instance.WriteError(methodName + Constants.Colon + ex.Message);
                throw new Exception(ex.ToString());
            }
        }

        /// <summary>
        /// Update to db SiteLog
        /// </summary>
        /// <param name="siteLog">Param SiteLog</param>
        public void Update(SiteLogEN siteLog)
        {
            string methodName = MethodBase.GetCurrentMethod().DeclaringType.ToString() + Constants.Dot + MethodBase.GetCurrentMethod().Name;
            LogHelper.Instance.WriteInfo(Constants.Begin + methodName);
            try
            {
                var sqlParameters = new[]
                                {
                                    new SqlParameter(Constants.SqlParamerterName.SiteLog.SiteLogId, siteLog.SiteLogId),
                                    new SqlParameter(Constants.SqlParamerterName.SiteLog.UserName, siteLog.UserName),
                                    new SqlParameter(Constants.SqlParamerterName.SiteLog.Email, siteLog.Email),
                                    new SqlParameter(Constants.SqlParamerterName.SiteLog.Browser, siteLog.Browser),
                                    new SqlParameter(Constants.SqlParamerterName.SiteLog.PlatForm, siteLog.PlatForm),
                                    new SqlParameter(Constants.SqlParamerterName.SiteLog.IpAddress, siteLog.IpAddress),
                                    new SqlParameter(Constants.SqlParamerterName.SiteLog.SubmitedTime, siteLog.SubmitedTime)
                                };

                SqlHelper.ExecuteNonQuery(SqlHelper.ConnectionString, Constants.StorepProcedure.VOneSiteLogUpdate, sqlParameters);
                LogHelper.Instance.WriteInfo(Constants.End + methodName);
            }
            catch (SqlException ex)
            {
                LogHelper.Instance.WriteError(methodName + Constants.Colon + ex.Message);
                throw new Exception(ex.ToString());
            }
        }

        /// <summary>
        /// Delete SiteLog by siteLogId
        /// </summary>
        /// <param name="siteLogId">param siteLogId</param>
        public void Delete(decimal siteLogId)
        {
            string methodName = MethodBase.GetCurrentMethod().DeclaringType.ToString() + Constants.Dot + MethodBase.GetCurrentMethod().Name;
            LogHelper.Instance.WriteInfo(Constants.Begin + methodName);
            try
            {
                var sqlParameters = new[] 
                { 
                    new SqlParameter(Constants.SqlParamerterName.SiteLog.SiteLogId, siteLogId)
                };

                SqlHelper.ExecuteNonQuery(SqlHelper.ConnectionString, Constants.StorepProcedure.VOneSiteLogDelete, sqlParameters);
                LogHelper.Instance.WriteInfo(Constants.End + methodName);
            }
            catch (SqlException ex)
            {
                LogHelper.Instance.WriteError(methodName + Constants.Colon + ex.Message);
                throw new Exception(ex.ToString());
            }
        }

        /// <summary>
        /// Delete All
        /// </summary>
        public void DeleteAll()
        {
            string methodName = MethodBase.GetCurrentMethod().DeclaringType.ToString() + Constants.Dot + MethodBase.GetCurrentMethod().Name;
            LogHelper.Instance.WriteInfo(Constants.Begin + methodName);
            try
            {
                SqlHelper.ExecuteNonQuery(SqlHelper.ConnectionString, Constants.StorepProcedure.VOneSiteLogDeleteAll);
                LogHelper.Instance.WriteInfo(Constants.End + methodName);
            }
            catch (SqlException ex)
            {
                LogHelper.Instance.WriteError(methodName + Constants.Colon + ex.Message);
                throw new Exception(ex.ToString());
            }
        }

        /// <summary>
        /// Gets all SiteLog paging.
        /// </summary>
        /// <param name="searchLogNP">param searchLogNP</param>
        /// <returns>return list SiteLogEN</returns>
        public IList<SiteLogEN> GetAllSiteLogPaging(SearchLogNP searchLogNP)
        {
            string methodName = MethodBase.GetCurrentMethod().DeclaringType.ToString() + Constants.Dot + MethodBase.GetCurrentMethod().Name;
            LogHelper.Instance.WriteInfo(Constants.Begin + methodName);
            try
            {
                IList<SiteLogEN> lstSiteLog = new List<SiteLogEN>();

                var sqlParameters = new[]
                                {
                                    new SqlParameter(Constants.SqlParamerterName.SiteLog.UserName, searchLogNP.UserName),
                                    new SqlParameter(Constants.SqlParamerterName.SiteLog.FromDate, searchLogNP.FromDate),
                                    new SqlParameter(Constants.SqlParamerterName.SiteLog.ToDate, searchLogNP.ToDate),
                                    new SqlParameter(Constants.SqlParamerterName.SiteLog.PageIndex, searchLogNP.PageIndex),
                                    new SqlParameter(Constants.SqlParamerterName.SiteLog.PageSize, searchLogNP.PageSize),
                                    new SqlParameter(Constants.SqlParamerterName.SiteLog.TotalRecords, searchLogNP.TotalRecords)
                                };

                sqlParameters[5].Direction = ParameterDirection.Output;
                
                SqlDataReader reader = SqlHelper.ExecuteReader(
                    SqlHelper.ConnectionString, CommandType.StoredProcedure, Constants.StorepProcedure.VOneSiteLogGetAllPaging, sqlParameters);
                
                while (reader.Read())
                {
                    var siteLog = new SiteLogEN();

                    if (!reader.IsDBNull(0))
                    {
                        siteLog.SiteLogId = reader.GetDecimal(0);
                    }

                    if (!reader.IsDBNull(1))
                    {
                        siteLog.UserName = reader.GetString(1);
                    }

                    if (!reader.IsDBNull(2))
                    {
                        siteLog.Email = reader.GetString(2);
                    }

                    if (!reader.IsDBNull(3))
                    {
                        siteLog.Browser = reader.GetString(3);
                    }

                    if (!reader.IsDBNull(4))
                    {
                        siteLog.PlatForm = reader.GetString(4);
                    }

                    if (!reader.IsDBNull(5))
                    {
                        siteLog.IpAddress = reader.GetString(5);
                    }

                    if (!reader.IsDBNull(6))
                    {
                        siteLog.SubmitedTime = reader.GetDateTime(6);
                    }

                    lstSiteLog.Add(siteLog);
                }

                reader.Close();

                searchLogNP.TotalRecords = Convert.ToInt32(sqlParameters[5].Value);

                reader.Dispose();
                LogHelper.Instance.WriteInfo(Constants.End + methodName);
                return lstSiteLog;
            }
            catch (SqlException ex)
            {
                LogHelper.Instance.WriteError(methodName + Constants.Colon + ex.Message);
                throw new Exception(ex.ToString());
            }
        }
    }
}